package com.quan.common.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.mzlion.easyokhttp.http.Header;
import com.quan.common.base.pojo.UserAgentVo;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;

public class UserAgentUtils {

	public static String getUserAgentString(HttpServletRequest request) {
		return request.getHeader(Header.USER_AGENT);
	}

	public static UserAgentVo getUserAgent(HttpServletRequest request) {
		return getUserAgent(getUserAgentString(request));
	}

	public static UserAgentVo getUserAgent(String userAgentString) {
		if (StringUtils.isBlank(userAgentString)) {
			return null;
		}

		UserAgent userAgent = new UserAgent(userAgentString);
		Browser browser = userAgent.getBrowser();
		Version browserVersion = userAgent.getBrowserVersion();
		OperatingSystem operatingSystem = userAgent.getOperatingSystem();

		UserAgentVo userAgentVo = new UserAgentVo(operatingSystem.getDeviceType().getName(), browser.getName(), (null != browserVersion ? browserVersion.getVersion() : null), browser.getRenderingEngine().name(), operatingSystem.getGroup().getName(), operatingSystem.getName());

		return userAgentVo;
	}

}
